package com.practice.springboot.rabbitmqtest.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.transaction.RabbitTransactionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description : fanout 交换机配置
 * @Version : V1.0.0
 * @Date : 2021/11/10 19:19
 */
@Configuration
public class FanoutRabbitMqConfig {
    /**
     * 创建三个队列 ：TestFanoutQueueA   TestFanoutQueueB  TestFanoutQueueC
     * 将三个队列都绑定在交换机 TestFanoutExchange 上
     * 因为是扇型交换机, 路由键无需配置,配置也不起作用
     */

    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("TestFanoutExchange", true, false);
    }

    // 配置启用rabbitmq事务
    @Bean
    public RabbitTransactionManager rabbitTransactionManager(ConnectionFactory connectionFactory) {
        return new RabbitTransactionManager(connectionFactory);
    }

    @Bean
    public Queue queueA() {
        return new Queue("TestQueueA", true);
    }

    @Bean
    public Queue queueB() {
        return new Queue("TestQueueB", true);
    }

    @Bean
    public Queue queueC() {
        return new Queue("TestQueueC", true);
    }

    // bind queue and exchange
    @Bean
    public Binding bindingA() {
        return BindingBuilder.bind(queueA()).to(fanoutExchange());
    }

    @Bean
    public Binding bindingB() {
        return BindingBuilder.bind(queueB()).to(fanoutExchange());
    }

    @Bean
    public Binding bindingC() {
        return BindingBuilder.bind(queueC()).to(fanoutExchange());
    }
}
